package org.tity.test.concurrent;

import java.util.Map;

import org.tity.commons.collection.MapUtil;
import org.tity.commons.log.Logger;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.OkHttpClient.Builder;
import okhttp3.Request;
import okhttp3.Response;

public class MDMAutoCode {

	
	static class PaTest extends Thread {
    	int port=-1;
    	
    	public PaTest(int port) {
    		this.port=port;
    	}
    	
    	@Override
    	public void run() {
    		for (int i = 1; i < 15000; i++) {
    			String msg="http://10.88.20.236:"+port+"/api/integration/autocode?modelId=2713&dataId="+i;
    			//msg="http://open.auxgroup.com/ding/recall/org/partner/get-root";
				String result="error";
				try {
					result = get(msg,"UTF-8",true,1);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				msg+="\n"+result;
				System.out.println(this.getName()+"\n"+msg);
			}
    	}
    	
    }
    
    public static void main(String[] args) {
		
    	for (int i = 0; i <20; i++) {
			(new PaTest(i%2==0?7001:7002)).start();
		}
    	
	}
    
    
    private static OkHttpClient client = null;
    
    private static Map<String,String> cookieValues=MapUtil.asMap("screenResolution","1536x864","JSESSIONID","c13f9fb4-8780-4d4c-8d32-d393fcbb7ff5");
    
    private static String get(String url, String encoding,boolean useCookie,int retry) {

    	if(client==null) {
    		 Builder builder=new Builder();
    		client=builder.build();
    	}
    	
		String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";

		Request.Builder builder=new Request.Builder();
		 builder.url(url).removeHeader("User-Agent")
				.addHeader("User-Agent", userAgent);
//				.addHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
//				.addHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8")
//				.addHeader("Cache-Control", "max-age=0")
//				.addHeader("Connection", "keep-alive")
//				.addHeader("Host", "www.10jqka.com.cn")
//				.addHeader("Upgrade-Insecure-Requests", "1")
//				.addHeader("Referer", "http://www.10jqka.com.cn/");
		 
		 		if(useCookie) {
		 			String cookieValue="";
		 			for (String name : cookieValues.keySet()) {
		 				cookieValue+=name+"="+cookieValues.get(name)+"; ";
					}
		 			if(cookieValue.length()>0) {
		 				cookieValue=cookieValue.substring(0,cookieValue.length()-2);
		 			}
		 			
		 			builder.addHeader("Cookie", cookieValue);
		 		}
				
				Request request =	builder.build();
 
		Call call = client.newCall(request);
		String content = null;
		try {
			Response response = call.execute();
			if (encoding == null) {
				content = response.body().string();
			} else {
				byte[] bs = response.body().bytes();
				content = new String(bs, encoding);
			}
		} catch (Exception e) {
			Logger.info("下载数据错误",e);
//			e.printStackTrace()
		}

		return content;

	}
}
